package py.org.financiero.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;


@FacesConverter("fechaConverter")
public class FechaConverter implements Converter {
	
	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		
		//Logger logger = LoggerFactory.getLogger(DateConverter.class);	
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		Date fecha = null;
		try {
			System.out.println("Creando objeto");
			fecha = sdf.parse(arg2);
		} catch(Exception ex) {
			ex.printStackTrace();
		}
		System.out.println("Retornando de DateConverter.getAsObject");
		return new Timestamp(fecha.getTime());
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		
		//Logger logger = LoggerFactory.getLogger(DateConverter.class);
		System.out.println("En DateConverter.getAsString " + arg2);
		String grupo = "";
		if(arg2 != null){
			System.out.println("Convirtiendo a string");
			if(arg2 instanceof java.sql.Timestamp){
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				grupo = sdf.format(arg2);
				//grupo = arg2.toString();
			}
			
		}
		System.out.println("Retornando DateConverter.getAsString");
		return grupo;
	}	
}
